package com.aurora.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.aurora.app.entity.StoryTopic;
import com.aurora.app.entity.ThingStory;
import com.aurora.app.entity.ThingStoryImage;
import com.aurora.app.entity.User;
import com.aurora.app.enums.FakeDelete;
import com.aurora.app.service.StoryTopicService;
import com.aurora.app.service.ThingStoryService;
import com.aurora.app.service.UserService;
import com.aurora.app.util.DateUtils;
import com.aurora.app.util.StandardResponseBody;

/**
 * 物语----文章
 * @author yongjiang.lin
 *
 */
@Controller
@RequestMapping(value = "api/thingStory")
public class ThingStoryController {
	@Resource
	private ThingStoryService thingStoryService;
	@Resource
	private StoryTopicService storyTopicService;
	
	@RequestMapping(value = "/getThingStory")
	@ResponseBody
	public String getThingStory(HttpServletRequest request,String thingStoryId){
		ThingStory thingStory = this.thingStoryService.get(thingStoryId);
		Map<String,Object> map = new  HashMap<String,Object>();
		if(thingStory == null|| thingStory.getIsDelete().equals(FakeDelete.NODELETE)){
			map.put("STATUS", "ERR");
			map.put("DESC", "无法该物语信息");
			return JSON.toJSONString(map);
		}
		map.put("ID", thingStory.getId());
		map.put("TITLE", thingStory.getTitle());
		map.put("CONTENT", thingStory.getContent());
		map.put("CREATETIME", DateUtils.getMyFormatTime(thingStory.getCreateTime()));
		map.put("ZAN", thingStory.getZan());
		map.put("VIEW", thingStory.getView());
		List<ThingStoryImage> imgList = thingStory.getImages();
		String imgs = "";
		if(imgList!=null&& !imgList.isEmpty()){
			for(ThingStoryImage thingStoryImage:imgList){
				imgs += thingStoryImage.getPath()+",";
			}
			imgs = imgs.substring(0,imgs.length()-1);
		}
		map.put("IMGS", imgs);
		map.put("STATUS", "OK");
		return JSON.toJSONString(map);
	}
	
	@RequestMapping(value = "/getTopicInfo")
	@ResponseBody
	public String getTopicInfo(HttpServletRequest request,String topicId){
		Map<String,Object> map = new  HashMap<String,Object>();
		StoryTopic topic = this.storyTopicService.get(topicId);
		if(topic!=null){
			map.put("NAME", topic.getName());
			map.put("TIME",topic.getCreateTime() );
			map.put("GUANZHU",topic.getLike() );
			map.put("STORY",topic.getStoryNum() );
			return StandardResponseBody.Default(1, "", map);
		}else{
			return StandardResponseBody.Default(0, "找不到该专题", null);
		}
	}
	
}
